package com.gos.creator.service.impl;

import com.gos.creator.domain.DataBase;
import com.gos.creator.domain.Table;
import com.gos.creator.domain.TableField;
import com.gos.creator.service.DocumentCreatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.List;

/**
 * 生成数据字典文档
 *
 * @author Xue Liang on 2019-08-24
 */
public class DocumentCreatorServiceImpl implements DocumentCreatorService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DocumentCreatorServiceImpl.class);

    @Override
    public boolean create(DataBase dataBase, String directory) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(directory + File.separator + "doc.html"))) {
            //按表生成
            List<Table> tables = dataBase.getTables();
            for (Table t : tables) {
                String name = t.getName();
                String desc = t.getDescription();
                if (StringUtils.hasText(desc)) {
                    desc = desc.replaceAll("\\s", " ");
                } else {
                    desc = "";
                }
                writer.append(desc).append("(").append(name).append(")<br/>");
                writer.append("<table cellspacing=\"0\" border=\"1\" style=\"border-collapse:collapse;\">");
                writer.append("<tr>")
                        .append("<td>字段名称</td>")
                        .append("<td>中文名称</td>")
                        .append("<td>数据类型</td>")
                        .append("<td>空值</td>")
                        .append("<td>说明</td>")
                        .append("</tr>");
                final List<TableField> fields = t.getFields();
                for (TableField field : fields) {
                    Integer maxLength = field.getMaxLength();
                    writer.append("<tr>")
                            .append("<td>")
                            .append(field.getTableFieldName())
                            .append("</td>")
                            .append("<td>")
                            .append(field.getDescription())
                            .append("</td>")
                            .append("<td>")
                            .append(field.getDataBaseFieldDataType())
                            .append(maxLength == null || maxLength < 1 ? "" : ("(" + field.getMaxLength() + ")"))
                            .append("</td>")
                            .append("<td>")
                            .append(field.getIsNull() ? "否" : "是")
                            .append("</td>")
                            .append("<td>")
                            .append("-")
                            .append("</td>")
                            .append("</tr>")
                            .append("\n");
                }
                writer.append("</table>")
                        .append("<br/><br/><br/><br/><br/>");
            }
            return true;
        } catch (IOException e) {
            LOGGER.error("error:", e);
            return false;
        }
    }
}
